﻿using System;
using System.Web;

namespace Landscape
{
    class WidgetTemplate : Template
    {
        public WidgetTemplate(string xmlTemplatePath) : base(xmlTemplatePath)
        {
            OutputMethod = Renderer.OutputMethods.Xml;
            Renderer.CreateXslOutput(TemplateDom, Renderer.OutputMethods.Xml, null, null, "utf-8", true, false, true, true);

            // create compiled transform object
            TemplateCompiledTransform = CreateCompiledTransform(TemplateDom);
        }

        public static WidgetTemplate GetTemplate(string templatePath)
        {
            if (AllowCache && HttpContext.Current != null && HttpContext.Current.Cache != null)
            {
                if (HttpContext.Current.Cache[templatePath] != null)
                {
                    WidgetTemplate cached = (WidgetTemplate)HttpContext.Current.Cache[templatePath];

                    return cached;
                }
            }

            WidgetTemplate template = new WidgetTemplate(templatePath);

            // saves object into cache
            if (AllowCache)
            {
                HttpContext.Current.Cache.Insert(templatePath, template, new System.Web.Caching.CacheDependency(template.DependenciesPaths.ToArray()));
            }

            return template;
        }
    }
}
